package com.home.common.framework.core.service.impl;

import com.home.base.core.framework.msg.PageResponse;
import com.home.common.framework.core.entity.EntityUtils;
import com.home.common.framework.core.service.BaseService;
import com.home.common.handler.exception.BizException;
import com.home.common.utils.query.QueryParams;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

/**
 * Description: 通用业务 service 实现类
 * Created by 王大宸 on 2020-09-28 20:16
 * Created with IntelliJ IDEA.
 */
public abstract class BaseServiceImpl<BaseMapper extends Mapper<T>, T, PK> implements BaseService<T, PK> {
    private static final Logger logger = LoggerFactory.getLogger(BaseServiceImpl.class);
    @Autowired
    protected BaseMapper baseMapper;

    /***
     * 功能说明：获取列表
     *
     * @author 王大宸
     * @date 2020/9/28 20:23
     * @param
     * @return java.util.List < T>
     */
    @Override
    public List<T> list(T entity) throws BizException {
        return baseMapper.select(entity);
    }

    /***
     * 功能说明：分页
     *
     * @author 王大宸
     * @date 2020/9/28 20:17
     * @param params page/limit
     * @return com.home.common.framework.msg.PageResponse
     */
    @Override
    public PageResponse<T> page(Map<String, Object> params) throws BizException {
        Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
        QueryParams query = new QueryParams(params);
        Example example = new Example(clazz);
        if (params.entrySet().size() > 0) {
            Example.Criteria criteria = example.createCriteria();
            for (Map.Entry<String, Object> entry : query.entrySet()) {
                criteria.orLike(entry.getKey(), entry.getValue() == null ? "" : "%" + entry.getValue().toString() + "%")
                        .orEqualTo(entry.getKey(), entry.getValue() == null ? "" : entry.getValue().toString());
            }
        }
        example.setOrderByClause("create_time DESC");
        Page<T> result = PageHelper.startPage(query.getPage(), query.getLimit());
        List<T> list = baseMapper.selectByExample(example);
        return PageResponse.ok(result.getTotal(), list);
    }

    /***
     * 功能说明：新增
     *
     * @author 王大宸
     * @date 2020/9/28 20:19
     * @param entity 新增数据
     * @return T
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public T saveInfo(T entity) throws BizException {
        EntityUtils.toCreatAndUpdateEntity(entity);
        logger.info("添加数据: " + entity.toString());
        baseMapper.insertSelective(entity);
        return entity;
    }

    /***
     * 功能说明：根据id查询数据
     *
     * @author 王大宸
     * @date 2020/9/28 20:20
     * @param id 需要查询数据的id
     * @return T
     */
    @Override
    public T getById(PK id) throws BizException {
        return baseMapper.selectByPrimaryKey(id);
    }

    /***
     * 功能说明：更新数据
     *
     * @author 王大宸
     * @date 2020/9/28 20:20
     * @param entity 更新数据
     * @return T
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public T updateInfo(T entity) throws BizException {
        EntityUtils.toUpdateEntity(entity);
        baseMapper.updateByPrimaryKeySelective(entity);
        return entity;
    }

    /***
     * 功能说明：删除
     *
     * @author 王大宸
     * @date 2020/9/28 20:22
     * @param id 需要删除数据的id
     * @return T
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(PK id) throws BizException {
        return baseMapper.deleteByPrimaryKey(id);
    }
}
